{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from scipy.stats.mstats import winsorize\n",
    "import statsmodels.api as sm\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import dautil as dl\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "starsCYG = sm.datasets.get_rdataset(\"starsCYG\", \"robustbase\", cache=True).data\n",
    "limit = 0.15\n",
    "winsorized_x = starsCYG.copy()\n",
    "winsorized_x['log.Te'] = winsorize(starsCYG['log.Te'], limits=limit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "winsorized_y = starsCYG.copy()\n",
    "winsorized_y['log.light'] = winsorize(starsCYG['log.light'], limits=limit)\n",
    "winsorized_xy = starsCYG.apply(winsorize, limits=[limit, limit])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('winsorising_data')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.label()\n",
    "sns.regplot(x='log.Te', y='log.light', data=starsCYG, ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.regplot(x='log.Te', y='log.light', data=winsorized_x, ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.regplot(x='log.Te', y='log.light', data=winsorized_y, ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.regplot(x='log.Te', y='log.light', data=winsorized_xy, ax=sp.ax)\n",
    "plt.tight_layout()\n",
    "HTML(dl.report.HTMLBuilder().watermark())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
